package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.*;
import com.itheima.mapper.*;
import com.itheima.pojo.PermissionPermissionGroup;
import com.itheima.pojo.pe.pe_permission;
import com.itheima.pojo.pe.pe_permission_group;
import com.itheima.pojo.pe.pe_permission_menu_extend;
import com.itheima.pojo.pe.pe_permission_point_extend;
import com.itheima.service.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
/*    @Autowired
    private PermissionsMapper permissionsMapper;
    @Autowired
    private APPGMapper appgMapper;
    @Autowired
    private PPGMapper ppgMapper;*/
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionMenuMapper menuMapper;
    @Autowired
    private PermissionPointMapper pointMapper;
/*    @Override
    public Result findpage(Integer page, Integer pagesize,String keyword) {
        Page<pe_permission_group> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<pe_permission_group>wrapper = new LambdaQueryWrapper<>();
        if (keyword!=null){
            wrapper.like(pe_permission_group::getName,keyword);
            Page<pe_permission_group>page1 = ppgMapper.selectPage(p,wrapper);
            return new Result(page1.getTotal(),page1.getSize(),page1.getPages(),page1.getCurrent(),page1.getRecords());
        }
        wrapper.like(pe_permission_group::getName,keyword);
        Page<pe_permission_group>page1 = ppgMapper.selectPage(p,wrapper);
        return new Result(page1.getTotal(),page1.getSize(),page1.getPages(),page1.getCurrent(),null);
    }


    @Override
    public void add(PermissionsDto permissionsDto) {
        pe_permission_group pp = new pe_permission_group();
        pp.setName(permissionsDto.getTitle());
        pp.setCreateTime(LocalDateTime.now());
        pp.setUpdateTime(LocalDateTime.now());
        ppgMapper.insert(pp);
        List<Integer> permissions = permissionsDto.getPermissions();
        for (Integer id : permissions) {
            PermissionPermissionGroup bb = new PermissionPermissionGroup();
            bb.setPgid(pp.getId());
            bb.setPid((long)id);
            appgMapper.insert(bb);
        }
    }

    @Override
    public void update(Integer id,PermissionsDto permissionsDto) {
        pe_permission_group pp = new pe_permission_group();
        pp.setUpdateTime(LocalDateTime.now());
        pp.setName(permissionsDto.getTitle());
        LambdaQueryWrapper<pe_permission_group>wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pe_permission_group::getId,id);
        ppgMapper.update(pp,wrapper);
        List<Integer> ids = permissionsDto.getPermissions();
        appgMapper.deleteBatchIds(ids);
        List<Integer> permissions = permissionsDto.getPermissions();
        for (Integer idd : permissions) {
            PermissionPermissionGroup bb = new PermissionPermissionGroup();
            bb.setPgid(pp.getId());
            bb.setPid((long) idd);
            appgMapper.insert(bb);
        }
    }

    @Override
    public void deleteBy(Integer id) {
        permissionsMapper.deleteById(id);
    }

    @Override
    public Result find(String keyword) {
        LambdaQueryWrapper<pe_permission_group>wrapper = new LambdaQueryWrapper<>();
        wrapper.like(pe_permission_group::getName,keyword);
        Page<pe_permission_group>page1 = ppgMapper.selectPage(null,wrapper);
        return new Result(page1.getTotal(),page1.getSize(),page1.getPages(),page1.getCurrent(),page1.getRecords());
    }

    @Override
    public Result xinxi(Integer id, PermissionsDto permissionsDto) {
        List<Integer> permissions = permissionsDto.getPermissions();
        for (Integer s : permissions) {
        LambdaQueryWrapper<pe_permission>wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pe_permission::getPid,s);
        List<pe_permission>list = permissionsMapper.selectList(wrapper);

        }


        return null;
    }*/

    @Override
    public List<MenusResult> findList() {

        List<pe_permission> pe_permissionList = permissionMapper.findMenus();
        for (int i = 0; i < pe_permissionList.size(); i++) {
            if (pe_permissionList.get(i) == null) {
                pe_permissionList.remove(i);
            }
        }
        List<MenusResult> menusResultList = new ArrayList<>();
        //权限->菜单->目录
        for (pe_permission permission : pe_permissionList) {
            //最外层,目录
            LambdaQueryWrapper<pe_permission> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission::getId, permission.getPid());
            pe_permission pe_permission = permissionMapper.selectOne(wrapper);
            //拿到code
            LambdaQueryWrapper<pe_permission_menu_extend> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(pe_permission_menu_extend::getPermissionId, pe_permission.getId());
            pe_permission_menu_extend pe_permission_menu_extend = menuMapper.selectOne(wrapper1);
            //封装数据
            MenusResult menusResult = new MenusResult();
            menusResult.setCode(pe_permission_menu_extend.getCode());
            menusResult.setId(pe_permission.getId());
            menusResult.setPid(pe_permission.getPid());
            menusResult.setTitle(pe_permission.getName());
            menusResultList.add(menusResult);
        }
        //第二层菜单项
        for (MenusResult menusResult : menusResultList) {
            LambdaQueryWrapper<pe_permission> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission::getPid, menusResult.getId());
            List<pe_permission> pe_permissionList1 = permissionMapper.selectList(wrapper);

            List<Child> childs = new ArrayList<>();
            for (pe_permission pe_permission : pe_permissionList1) {
                //拿到code
                LambdaQueryWrapper<pe_permission_menu_extend> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(pe_permission_menu_extend::getPermissionId, pe_permission.getId());
                pe_permission_menu_extend pe_permission_menu_extend = menuMapper.selectOne(wrapper1);
                //封装数据
                Child child = new Child();
                child.setCode(pe_permission_menu_extend.getCode());
                child.setId(pe_permission.getId());
                child.setPid(pe_permission.getPid());
                child.setTitle(pe_permission.getName());
                childs.add(child);
            }
            //权限点
            for (int i = 0; i < childs.size(); i++)  {
                LambdaQueryWrapper<pe_permission> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(pe_permission::getType, 2);
                wrapper1.eq(pe_permission::getPid, childs.get(i).getId());
                List<pe_permission> ppList = permissionMapper.selectList(wrapper1);
                if (ppList.size() != 0) {
                    //复制数据
                    ChildPoint childPoint = new ChildPoint();
                    BeanUtils.copyProperties(childs.get(i),childPoint);
                    List<Point> points = new ArrayList<>();
                    for (int j = 0; j < ppList.size(); j++) {
                        LambdaQueryWrapper<pe_permission_point_extend> wrapper2 = new LambdaQueryWrapper<>();
                        wrapper2.eq(pe_permission_point_extend::getPermissionId,ppList.get(j).getId());
                        pe_permission_point_extend pointExtend = pointMapper.selectOne(wrapper2);
                        //封装数据
                        Point point = new Point();
                        point.setCode(pointExtend.getCode());
                        point.setId(ppList.get(j).getId());
                        point.setPid(ppList.get(j).getPid());
                        point.setTitle(ppList.get(j).getName());
                        point.setIs_point(true);
                        points.add(point);
                    }
                    childPoint.setPoints(points);
                    childs.remove(i);
                    childs.add(childPoint);
                }
            }
            menusResult.setChilds(childs);
        }
        return menusResultList;
    }

    @Override
    public Point findMenus(Long id) {
        pe_permission pe_permission = permissionMapper.selectById(id);
        Point point = new Point();
        point.setId(pe_permission.getId());
        point.setTitle(pe_permission.getName());
        point.setPid(pe_permission.getPid());
        if (pe_permission.getType() == 1) {//菜单项
            LambdaQueryWrapper<pe_permission_menu_extend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission_menu_extend::getPermissionId, pe_permission.getId());
            pe_permission_menu_extend menuExtend = menuMapper.selectOne(wrapper);
            point.setCode(menuExtend.getCode());
            point.setIs_point(false);
        }
        if (pe_permission.getType() == 2) {//权限点
            LambdaQueryWrapper<pe_permission_point_extend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission_point_extend::getPermissionId, pe_permission.getId());
            pe_permission_point_extend pointExtend = pointMapper.selectOne(wrapper);
            point.setCode(pointExtend.getCode());
            point.setIs_point(true);
        }
        return point;
    }

    @Override
    public void modify(Long id, Point point) {
        pe_permission pe_permission = permissionMapper.selectById(id);
        if (point.isIs_point()){//权限点
            pe_permission.setName(point.getTitle());
            pe_permission.setPid(point.getPid());
            permissionMapper.updateById(pe_permission);
            LambdaQueryWrapper<pe_permission_point_extend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission_point_extend::getPermissionId,id);
            pe_permission_point_extend pointExtend = pointMapper.selectOne(wrapper);
            pointExtend.setCode(point.getCode());
            pointMapper.updateById(pointExtend);
            return;
        }
        //菜单项
        pe_permission.setName(point.getTitle());
        pe_permission.setPid(point.getPid());
        permissionMapper.updateById(pe_permission);
        LambdaQueryWrapper<pe_permission_menu_extend> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pe_permission_menu_extend::getPermissionId,id);
        pe_permission_menu_extend menuExtend = menuMapper.selectOne(wrapper);
        menuExtend.setCode(point.getCode());
        menuMapper.updateById(menuExtend);
    }

    @Override
    public void insert(Point point) {
        pe_permission pe_permission = new pe_permission();
        pe_permission.setPid(point.getPid());
        pe_permission.setName(point.getTitle());
        pe_permission.setCreateTime(LocalDateTime.now());
        permissionMapper.insert(pe_permission);
        if (point.isIs_point()){//权限点
            pe_permission_point_extend pointExtend = new pe_permission_point_extend();
            pointExtend.setPermissionId(pe_permission.getId());
            pointExtend.setCode(point.getCode());
            pointMapper.insert(pointExtend);
            pe_permission.setPermissionPointExtendId(pointExtend.getId());
            pe_permission.setType(2);
            permissionMapper.updateById(pe_permission);
            return;
        }
        //菜单项
        pe_permission_menu_extend menuExtend = new pe_permission_menu_extend();
        menuExtend.setPermissionId(pe_permission.getId());
        menuExtend.setCode(point.getCode());
        menuMapper.insert(menuExtend);
        pe_permission.setPermissionMenuExtendId(menuExtend.getId());
        pe_permission.setType(1);
        permissionMapper.updateById(pe_permission);
    }

    @Override
    public void remove(Long id) {
        //先判断是菜单还是权限点
        pe_permission pe_permission = permissionMapper.selectById(id);
        if (pe_permission.getType()==1){//菜单项
            LambdaQueryWrapper<pe_permission_menu_extend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission_menu_extend::getPermissionId,id);
            menuMapper.delete(wrapper);
        }
        if (pe_permission.getType()==2){//权限点
            LambdaQueryWrapper<pe_permission_point_extend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(pe_permission_point_extend::getPermissionId,id);
            pointMapper.delete(wrapper);
        }
        permissionMapper.deleteById(id);
    }

}
